<?php

namespace app\common\model\redis;

use app\admin\model\goods\GoodsSku;
use app\admin\model\goods\GoodsSpec;
use think\Log;
use think\Model;
use app\admin\model\goods\Goods;

/**
 * 商品秒杀防并发模型
 */
class Seckill
{
    protected $_redis = null;

    public function _initialize()
    {
//        $this->_redis = redis();
    }


    /**
     * 访问产品前先将当前产品库存队列
     */
    public function _before_detail($goods_id)
    {
        // 判断商品各规格库存量
        $sku_where['goods_id'] = $goods_id;
        $sku_where['spec_stock'] = ['>', 0];
        $goods_sku = GoodsSpec::where($sku_where)->field('goods_spec_id,spec_stock')->select();
        if (!empty($goods_sku)) {
            $goods_sku = collection($goods_sku)->toArray();

            $redis = redis()->handler();
            foreach ($goods_sku as $key => $val) {
                // 商品规格库存队列
                $goods_number_key = 'goods_sku_' . $val['goods_spec_id'];

                if ($redis->exists($goods_number_key) == 0) {
                    $res = $redis->set($goods_number_key, $val['spec_stock']);
                    if (!$res) {
                        return 10003;
                    }
                } else {
                    $goods_number = $redis->get($goods_number_key);
                    if ($goods_number <= 0) {
                        $res = $redis->set($goods_number_key, $val['spec_stock']);
                        if (!$res) {
                            return 10003;
                        }
                    }
                }
            }
        } else {
            return 49101;
        }

        return 1;

    }

    /**
     * 访问产品前先将当前课程库存队列
     */
    public function __before_detail($course_id, $stock)
    {
        $redis = redis()->handler();
        // 商品规格库存队列
        $course_number_key = 'course_' . $course_id;

        if ($redis->exists($course_number_key) == 0) {
            $res = $redis->set($course_number_key, $stock);
//            if(!$res){
//                return 48209;
//            }
        } else {
            $course_number = $redis->get($course_number_key);
            if ($course_number <= 0) {
                $res = $redis->set($course_number_key, $stock);
//                if(!$res){
//                    return 48209;
//                }
            }
        }
        return 1;
    }

    /**
     * todo 普通商品防并发处理 / 秒杀活动单个用户可重复购买
     * 抢购商品前处理当前会员是否进入队列
     * @param Int $user_id
     * @param Int $sku_id
     * @param Int $num
     */
    public function goods_number_queue($user_id, $sku_id, $num)
    {
        // 用户购买集合
        $user_queue_key = 'goods_sku_' . $sku_id . '_user';
        // 商品规格库存集合
        $goods_number_key = 'goods_sku_' . $sku_id;

        $redis = redis();

        $dec_num = $redis->dec($goods_number_key, $num);

        if ($dec_num < 0) {
//            file_put_contents('kill.txt','库存不足'.$user_id.PHP_EOL,FILE_APPEND);
//            Log::write('库存不足'.$user_id.PHP_EOL);
//            echo '库存不足'.$user_id.PHP_EOL;
            return false;
        } else {
//            file_put_contents('kill.txt','success'.$user_id.PHP_EOL,FILE_APPEND);
            $redis->handler()->sAdd($user_queue_key, $user_id);
//            Log::write('success'.$user_id.PHP_EOL);
//            echo 'success'.$user_id.PHP_EOL;
            return true;
        }
    }

    /**
     * todo 普通商品防并发处理 / 秒杀活动单个用户可重复购买
     * 抢购商品前处理当前会员是否进入队列
     * @param Int $user_id
     * @param Int $course_id
     * @param Int $num
     */
    public function course_number_queue($course_id, $num)
    {
        // 课程库存集合
        $course_number_key = 'course_' . $course_id;

        $redis = redis();

        $dec_num = $redis->dec($course_number_key, $num);

        if ($dec_num < 0) {
//            file_put_contents('kill.txt','库存不足'.$user_id.PHP_EOL,FILE_APPEND);
//            Log::write('库存不足'.$user_id.PHP_EOL);
//            echo '库存不足'.$user_id.PHP_EOL;
            return false;
        } else {
//            file_put_contents('kill.txt','success'.$user_id.PHP_EOL,FILE_APPEND);
//            Log::write('success'.$user_id.PHP_EOL);
//            echo 'success'.$user_id.PHP_EOL;
            return true;
        }
    }

    /**
     * todo 开启秒杀活动单个用户不可重复购买
     * 抢购商品前处理当前会员是否进入队列
     * @param  $user_id
     * @param  $goods_id
     * @param  $sku_id
     * @param  $num
     */
    public function kill_goods_number_queue($user_id, $goods_id, $sku_id, $num)
    {
        // 商品秒杀队列
        $user_queue_key = 'goods_sku_' . $sku_id . '_user';
        // 商品规格库存队列
        $goods_number_key = 'goods_sku_' . $sku_id;

        /* redis 队列 */
        $redis = redis()->handler();
        /* 进入队列 */
        $goods_number_key_res = $redis->llen($goods_number_key);

        //当商品可购买数大于1的时候判断库存队列长度
        if ($goods_number_key_res) {
            if ($goods_number_key_res < $num) {
                return false;
            }
        }

        if (!$redis->hGet($user_queue_key, $user_id)) {
            // 当商品可购买数大于1的时候,循环出栈
            for ($i = 0; $i < $num; $i++) {
                $goods_number_key_res = $redis->lpop($goods_number_key);
            }
        }

        // 设置单个用户不可重复购买
        if ($goods_number_key_res) {
            // 判断用户是否已在队列
            if (!$redis->hGet($user_queue_key, $user_id)) {
                // 插入抢购用户信息
                $userinfo = array(
                    "user_id" => $user_id,
                    "create_time" => time()
                );
                $redis->hSet($user_queue_key, $user_id, serialize($userinfo));

                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public function clearRedis($user_id, $sku_id)
    {
        set_time_limit(0);
        $redis = redis()->handler();
        // todo 商品秒杀队列d
        //$user_queue_key = 'goods_sku_'.$sku_id.'_user';
        // todo 商品规格库存队列
        //$goods_number_key = 'goods_sku_'.$sku_id;
        //$Rd = $redis->del($user_queue_key);
        //$a = $redis->hGet($user_queue_key, $user_id);
        $Rd = $redis->hDel("goods49", '用户id');
        $a = $redis->hGet("goods_49_user", '用户id');
        if (!$a) {
            dump($a);
        }

        if ($Rd == 0) {
            exit("Redis队列已释放！");
        }
    }


}
